package bbs2.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import bbs2.model.Board;

public class BoardDAO {

    private static Board makeBoard(ResultSet rs) throws Exception {
        Board board = new Board();
        board.setId(rs.getInt("ID"));
        board.setTitle(rs.getString("title"));
        board.setParentId(rs.getInt("parentID"));
        board.setCommentEnabled(rs.getBoolean("commentEnabled"));
        board.setFileAttachEnabled(rs.getBoolean("fileAttachEnabled"));
        return board;
    }
    
    public static Board selectById(int id) throws Exception {
        Board board = null;
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;        
        try {
            connection = DB.getConnection();
            String sql = "SELECT * FROM [Board] WHERE [id] = ?"; 
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            resultSet = statement.executeQuery();
            if (resultSet.next()) board = makeBoard(resultSet);
        } finally {
            if (resultSet != null) resultSet.close();
            if (statement != null) statement.close();
            if (connection != null) connection.close();
        }
        return board;
    }
    
}